package com.ww.springboot.boot.algorithm.tree;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 描述：无法画出来的原因：数据过大时 空间调整不好控制 数据容易重叠
 *
 * @author 🧑 ‍wanwei
 * @since 2022-01-08 10:39
 */
public class TreePaint extends JFrame {

    private static java.util.List<Integer> arrays = new ArrayList<>();
    private static TreeNode treeNode = new TreeNode();

    public TreePaint() throws InterruptedException {
        add(new TreePaint.Composition());

        new Thread(()->{
            try {
                method();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }

    private void method() throws InterruptedException {

        for(int i = 1;i<=6;i++){
            Thread.sleep(1000);
            arrays.add(i);
            TwoTreeDemo4<Integer> twoTreeDemo4 = new TwoTreeDemo4();
            treeNode = twoTreeDemo4.buildTree(arrays);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new TreePaint();
        frame.setTitle("二叉树");
        frame.setSize(830, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }


    class Composition extends JPanel {

        //定时刷新
        public Composition() {
            Timer timer = new Timer(500, new TreePaint.Composition.Refresh());
            timer.start();
        }


        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = 300;
            int y = 100;

            //左遍历
            leftErgodic(treeNode, x, y, g);
        }

        class Refresh implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                //重画
                repaint();
            }
        }
    }

    /**
     * 左序遍历
     *
     * @param treeNode
     */
    private static void leftErgodic(TreeNode treeNode, int x, int y, Graphics g) {
        if (treeNode == null) {
            return;
        }

        System.out.println(treeNode.getVal());
        TreePaint.paintLeft(g, x, y, String.valueOf(treeNode.getVal()));
        leftErgodic(treeNode.getLeft(), x - 40, y + 52, g);
        TreePaint.paintRight(g, x, y, String.valueOf(treeNode.getVal()));
        leftErgodic(treeNode.getRight(), x + 40, y + 52, g);
    }

    /**
     * 中序遍历
     *
     * @param treeNode
     */
    private void getMiddleValue(TreeNode treeNode) {

    }

    /**
     * 右序遍历
     *
     * @param treeNode
     */
    private void getRightValue(TreeNode treeNode) {

    }

    private static void paintLeft(Graphics g, int x, int y, String text) {
        //画圆
        g.drawOval(x, y, 20, 20);
        //画数字
        g.drawString(text, x + 4, y + 15);
        //画线左
        g.drawLine(x, y + 20, x - 30, y + 20 + 40);
    }

    private static void paintRight(Graphics g, int x, int y, String text) {
        //画圆
        g.drawOval(x, y, 20, 20);
        //画数字
        g.drawString(text, x + 4, y + 15);
        //画线右
        g.drawLine(x + 20, y + 20, x + 20 + 30, y + 20 + 40);
    }


}
